import 'package:flutter/cupertino.dart';
import 'package:flutter_rider/kit/oss_kit.dart';
import 'package:flutter_rider/main/widgets/dialog/alert_dialog.dart';
import 'package:flutter_rider/main/widgets/dialog/loading_dialog.dart';
import 'package:flutter_rider/utils/tip.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

class UploadImageHelper {
  final Map<String, String> _imagesMap = {};
  final BuildContext context;
  final int maxImages;
  final ValueNotifier<List<AssetEntity>> _valueNotifier = ValueNotifier([]);

  UploadImageHelper(this.context, {this.maxImages = 3});

  // void upload({required Function success, required Function fail}) {
  //   if (_valueNotifier.value.isEmpty) {
  //     success();
  //   } else {
  //     _uploadImages(0, success, fail);
  //   }
  // }

  ValueNotifier<List<AssetEntity>> getValueNotifier() {
    return _valueNotifier;
  }

  List<AssetEntity> getImages() {
    return _valueNotifier.value;
  }

  void remove(AssetEntity item) {
    _valueNotifier.value.remove(item);
    _valueNotifier.value = [..._valueNotifier.value];
  }

  setImages(List<AssetEntity> list) {
    _valueNotifier.value.clear();
    _valueNotifier.value.addAll(list);
  }

  List<String> getImageUrls() {
    List<String> jImages = [];
    for (var element in _valueNotifier.value) {
      jImages.add(_imagesMap[element.id]!);
    }
    return jImages;
  }

  // _uploadImages(int index, Function success, Function fail) async {
  //   //上传成功过一次
  //   if(_imagesMap.containsKey([_valueNotifier.value[index].id])) {
  //     if (index < _valueNotifier.value.length - 1) {
  //       _uploadImages(index + 1, success, fail);
  //     } else {
  //       success();
  //     }
  //     return;
  //   }
  //   var file = await _valueNotifier.value[index].originFile;
  //   //新版本上传
  //   OssKit.instance.upload(file!, (url) {
  //     _imagesMap[_valueNotifier.value[index].id] = url;
  //     if (index < _valueNotifier.value.length - 1) {
  //       _uploadImages(index + 1, success, fail);
  //     } else {
  //       success();
  //     }
  //   }, () {
  //     fail();
  //   },);
  // }

  void upload(List<AssetEntity> assets,
      {required Function success, required Function fail}) {
    _uploadImages(0, assets, success, fail);
  }

  ///修改成选择完图片就上传
  _uploadImages(int index, List<AssetEntity> assets, Function success,
      Function fail) async {
    //上传成功过一次
    if (_imagesMap.containsKey([assets[index].id])) {
      if (index < assets.length - 1) {
        _uploadImages(index + 1, assets, success, fail);
      } else {
        success();
      }
      return;
    }
    var file = await assets[index].originFile;
    //新版本上传
    OssKit.instance.upload(
      file!,
      (url) {
        _imagesMap[assets[index].id] = url;
        if (index < assets.length - 1) {
          // Tip.show("图片上传${index}");
          _uploadImages(index + 1, assets, success, fail);
        } else {
          success();
        }
      },
      () {
        fail();
      },
    );
  }

  pickImage() async {
    if (_valueNotifier.value.length >= maxImages) {
      CustomAlertDialog.alert(context, content: "最多只能上传$maxImages张照片");
      return;
    }
    final List<AssetEntity>? assets = await AssetPicker.pickAssets(context,
        maxAssets: maxImages - _valueNotifier.value.length);
    //下面这句是适配新版本的，但是没办法有问题 所以还是还原到flutter 2.X的版本
    // await AssetPicker.pickAssets(context,pickerConfig: AssetPickerConfig(maxAssets:maxImages,selectedAssets:_valueNotifier.value), );
    if (assets != null) {
      var dialog = LoadingDialog(context);
      dialog.show(text: "图片上传中...");
      upload(assets, success: () {
        dialog.hide();
        _valueNotifier.value.addAll(assets);
        _valueNotifier.value = [..._valueNotifier.value];
      }, fail: () {
        dialog.hide();
        Tip.show("图片上传失败");
      });
    }
  }

  takePhoto() async {
    if (_valueNotifier.value.length >= maxImages) {
      CustomAlertDialog.alert(context, content: "最多只能上传$maxImages张照片");
      return;
    }
    final AssetEntity? entity = await CameraPicker.pickFromCamera(context);
    if (entity != null) {
      var dialog = LoadingDialog(context);
      dialog.show(text: "图片上传中...");
      upload([entity], success: () {
        dialog.hide();
        _valueNotifier.value.add(entity);
        _valueNotifier.value = [..._valueNotifier.value];
      }, fail: () {
        dialog.hide();
        Tip.show("图片上传失败");
      });
    }
  }
}
